﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace SLEnvironmentSystem
{
    public class EnvironmentArea:MonoBehaviour
    {
        public event Action OnChanged;

        public string Tag;

        List<GameObject> gos = new List<GameObject>();
        BoxCollider collider=null;
        public void SetSize(Vector3 size) {
            if (collider==null)
            {
                collider = gameObject.AddComponent<BoxCollider>();
                collider.isTrigger = true;
            }
            collider.size = size;
        }

        private void OnTriggerEnter(Collider other)
        {
            if (string.IsNullOrEmpty(Tag))
            {
                gos.Add(other.gameObject);
            }
            else
            {
                if (other.tag == Tag)
                {
                    gos.Add(other.gameObject);
                }
            }

            if (OnChanged != null)
            {
                OnChanged();
            }
        }

        private void OnTriggerExit(Collider other)
        {
            if (string.IsNullOrEmpty(Tag)) 
            {
                gos.Remove(other.gameObject);
            }
            else
            {
                if (other.tag == Tag)
                {
                    gos.Remove(other.gameObject);
                }
            }
            
            if (OnChanged != null)
            {
                OnChanged();
            }
        }

        public List<GameObject> GetGosInArea()
        {
            return gos;
        }
    }
}
